﻿using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;

namespace KL.UFrame.Core.Configuration
{
    /// <summary>
    /// 全局配置文件
    /// </summary>
    public static class AppConfiguration
    {
        #region App Settings

        /// <summary>
        /// 服务器绑定的域名或者IP
        /// </summary>
        public static readonly string Host = ConfigurationManager.AppSettings["host"];

        #endregion

        #region 构造函数

        static AppConfiguration()
        {
            
        }

        #endregion

        #region 常量

        /// <summary>
        /// Http请求中，存储加密token的header名
        /// </summary>
        public const string OpenIdHeaderName = "X-ACCESS_TOKEN";

        /// <summary>
        /// 文件上传根目录
        /// </summary>
        public const string FileUploadPath = "/Upload/";

        /// <summary>
        /// 手机号正则
        /// </summary>
        public static readonly Regex PhoneRegex = new Regex(@"^1\d{10}$");

        /// <summary>
        /// 用户默认头像
        /// </summary>
        public const string DefaultUserHeadImageUrl = "img/avatar.png";

        /// <summary>
        /// 检查是否包特殊字符:仅允许中文、英文和数字
        /// </summary>
        public const string StringValidateRegexString = @"^(\w|[\u4E00-\u9FA5])*$";

        /// <summary>
        /// 检查是否包特殊字符:仅允许中文、英文和数字
        /// </summary>
        public static readonly Regex StringValidateRegex = new Regex(StringValidateRegexString);

        /// <summary>
        /// 检查是否有<
        /// </summary>
        public static readonly Regex ContentValidateRegex = new Regex(@"^[^<]*$");


        #endregion

        #region Url

        #endregion
        #region 全局方法

        /// <summary>
        /// 返回url的绝对路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetAbsoluteUrl(string url)
        {
            if (string.IsNullOrEmpty(url)) return url;

            if (url.StartsWith("http://") || url.StartsWith("https://")) return url;

            return Host + (url.StartsWith("/") ? url : ("/" + url));
        }

        /// <summary>
        /// 获取用户验证码缓存Key
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string GetValidationKey(string phone) => "ValidationKey_" + phone + "_";

        #endregion

    }
}